package com.example.administrator.cycleimage;

import android.content.Context;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import android.widget.ImageView;


import com.bumptech.glide.Glide;

import com.example.administrator.cycleimage.wedgit.BannerImageViewUtils;
import com.example.administrator.cycleimage.wedgit.CycleViewPager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private CycleViewPager mCycleViewPager;
    private List<String> mImgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        OkHttpClient mOkHttpClient = new OkHttpClient();
        Request mRequest = new Request.Builder().url("http://192.168.8.4/tp3/api.php/Home/Goods/getBanner").build();
        mOkHttpClient.newCall(mRequest).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonStr = response.body().string();
                Log.d("-----", "获取的数据是 -- " + jsonStr);
                try {
                    JSONObject jsonObject = new JSONObject(jsonStr);
                    JSONArray jsonArray = jsonObject.getJSONArray("data");

                    mImgs = new ArrayList<String>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        mImgs.add(jsonArray.getString(i));
                    }

                    Log.d("-----", "解析结果 -- " + mImgs.toString());
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setBanner(mImgs);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        mCycleViewPager = (CycleViewPager) getSupportFragmentManager().findFragmentById(R.id.life_fragment_banner);
        initBanner();
    }

    //由banner工厂生产出来的imageview
    private ImageView bannerImageView;
    private List<ImageView> mImageViews = new ArrayList<>();

    private void initBanner() {
        bannerImageView = BannerImageViewUtils.getImageView(mContext);

        bannerImageView.setImageResource(R.drawable.life_banner);
        mImageViews.add(bannerImageView);

        // 设置循环，在调用setData方法前调用
        mCycleViewPager.setCycle(false);
        // 在加载数据前设置是否循环
        mCycleViewPager.setData(mImageViews, null, null);
        //设置轮播
        mCycleViewPager.setWheel(false);
        //设置圆点指示图标组居中显示，默认靠右
        mCycleViewPager.setIndicatorCenter();
        mImageViews.clear();
    }

    private void setBanner(List<String> mImgs) {
        mImageViews.clear();

        for (String banner : mImgs) {
            bannerImageView = BannerImageViewUtils.getImageView(mContext);
            Glide.with(mContext).load(banner).centerCrop()
                    .placeholder(R.drawable.life_banner)
                    .crossFade().into(bannerImageView);
            mImageViews.add(bannerImageView);
        }

        //重新添加第一个view
        bannerImageView = BannerImageViewUtils.getImageView(mContext);
        Glide.with(mContext).load(mImgs.get(mImgs.size() - 1)).centerCrop()
                .placeholder(R.drawable.life_banner)
                .crossFade().into(bannerImageView);
        mImageViews.add(0, bannerImageView);
        //重新添加最后一个view
        bannerImageView = BannerImageViewUtils.getImageView(mContext);
        Glide.with(mContext).load(mImgs.get(0)).centerCrop()
                .placeholder(R.drawable.life_banner)
                .crossFade().into(bannerImageView);
        mImageViews.add(bannerImageView);

        // 设置循环，在调用setData方法前调用
        mCycleViewPager.setCycle(true);
        // 在加载数据前设置是否循环
        mCycleViewPager.setData(mImageViews, mImgs, null);
        //设置轮播
        mCycleViewPager.setWheel(true);
        // 设置轮播时间，默认5000ms
        mCycleViewPager.setTime(2000);
        //设置圆点指示图标组居中显示，默认靠右
        mCycleViewPager.setIndicatorCenter();
    }
}
